#include <limits>

double sum(double a, double b, double & roundoff)
{
        double res = a + b;
        double bv = res - a;
        double av = res - bv;
        double br = b - bv;
        double ar = a - av;
        roundoff = ar + br;
        return res;
}

void split(double a, std::size_t s, double & hi, double & lo)
{
        double c = ((1LL << s) + 1LL) * a;
        double ab = c - a;
        hi = c - ab;
        lo = a - hi;
}

double mul(double a, double b, double & roundoff)
{
        double res = a * b;
        std::size_t s = std::numeric_limits<double>::digits / 2 + std::numeric_limits<double>::digits % 2;
        double a_hi, a_lo, b_hi, b_lo;
        split(a, s, a_hi, a_lo);
        split(b, s, b_hi, b_lo);
        double e1 = res - (a_hi * b_hi);
        double e2 = e1 - (a_lo * b_hi);
        double e3 = e2 - (b_lo * a_hi);
        roundoff = (a_lo * b_lo) - e3;
        return res;
}

